package com.shiger.pingworld;


import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class ListViewAdapter extends BaseAdapter {
    private String TAG = "ListViewAdapter";
    Context context;
    List<String> mUrls;
    private OnItemLongClickLister mOnItemLongClickLister;

    public ListViewAdapter(){}

    public ListViewAdapter(Context context , List<String> infos ){
        this.context = context;
        this.mUrls = infos;
    }

    public void updateDatas(List<String> urls){
        mUrls = urls;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public List<String> getStrings() {
        return mUrls;
    }

    public void setStrings(List<String> appInfos) {
        this.mUrls = appInfos;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        int count = 0;
        if(null != mUrls){
            return mUrls.size();
        };
        return count;
    }

    @Override
    public Object getItem(int index) {
        // TODO Auto-generated method stub
        return mUrls.get(index);
    }

    @Override
    public long getItemId(int index) {
        // TODO Auto-generated method stub
        return index;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub
        Log.d(TAG, "getView: position = " + position);
         ViewHolder viewHolder = null;
        if(null == convertView){
            viewHolder = new ViewHolder();
            LayoutInflater mInflater = LayoutInflater.from(context);
            convertView = mInflater.inflate(R.layout.listview_item, null);
            viewHolder.urlText = (TextView)convertView.findViewById(R.id.url_name);
            viewHolder.speedText = (TextView)convertView.findViewById(R.id.speed_value);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)convertView.getTag();
        }
        if(null != mUrls){
            String url = mUrls.get(position);
            viewHolder.urlText.setText(url);
            final ViewHolder finalViewHolder = viewHolder;
            Model.getInstance().loadPingResult(url, new Model.OnPingFinishedLister() {
                @Override
                public void onPingFinished(String result) {
                    finalViewHolder.speedText.setText(result);
                }
            });
//            viewHolder.speedText.setText(packageName);

            if(mOnItemLongClickLister !=null)
            {
                convertView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        mOnItemLongClickLister.onItemLongClick(position);
                        return false;
                    }
                });
            }

        }

        return convertView;
    }


    private class ViewHolder{
        ImageView appIconImg;
        TextView urlText;
        TextView speedText;
    }

    public String getLauncherActivityNameByPackageName(Context context, String packageName) {
        String className = null;
        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);//android.intent.action.MAIN

        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resolveIntent.setPackage(packageName);
        List<ResolveInfo> resolveinfoList = context.getPackageManager().queryIntentActivities(resolveIntent, 0);

        if(resolveinfoList.size() == 0){
            resolveIntent = new Intent();
            resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            resolveIntent.setPackage(packageName);
            resolveinfoList = context.getPackageManager().queryIntentServices(resolveIntent, 0);

            if(resolveinfoList.size() == 0){
                return "";
            }

            ResolveInfo resolveinfo = resolveinfoList.iterator().next();
            if (resolveinfo != null) {
                className = resolveinfo.serviceInfo.name+"  -----is a service";
                return className;
            }
        }

        Log.d(TAG , resolveinfoList.toString());
        try {
            ResolveInfo resolveinfo = resolveinfoList.iterator().next();
            if (resolveinfo != null) {
                className = resolveinfo.activityInfo.name;
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }

        return className;
    }

    public void setOnLongClick(OnItemLongClickLister onItemLongClickLister){
        mOnItemLongClickLister = onItemLongClickLister;
    }

    public interface OnItemLongClickLister{
        void onItemLongClick(int position);
    }
}
